package com.itcam.reflect;

/**
 * @author : Cammy.Wu
 * Description : 测试Class类的创建方式有哪些
 */

public class Test03 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是：" + person.name);


        // 方式一：通过一个对象的getClass()方法获取
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        // 方式二：通过Class类的静态方法forName(String className)获取
        Class c2 = Class.forName("com.itcam.reflect.Student");
        System.out.println(c2.hashCode());

        // 方式三：通过类的class属性获取
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        // 方式四：基本内置类型的包装类都有一个TYPE属性
        Class<Integer> c4 = Integer.TYPE;
        System.out.println(c4);

        // 获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);
    }
}

class Person {
    String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Student extends Person {
    public Student() {
        this.name = "student";
    }
}

class Teacher extends Person {
    public Teacher() {
        this.name = "teacher";
    }
}